package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.model.Keyword;
import com.cskaoyan.model.KeywordExample;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.service.KeywordService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: KeywordServiceImpl
 * @Description: TODO 关键词管理模块
 * @Author:zhoukang
 * @Date: 2022/5/6 14:32
 * @Version: 1.0
 **/
@Transactional
@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    KeywordMapper keywordMapper;


    /**
     * @author: zhoukang
     * @description: TODO 查询keywrod列表
     * @date: 2022/5/6 14:45
     */
    @Override
    public DataVO<Keyword> query(BaseParamBO param, String keyword, String url) {
        Integer limit = param.getLimit();
        Integer page = param.getPage();
        String order = param.getOrder();
        String sort = param.getSort();
        //分页
        PageHelper.startPage(page,limit);
        //执行查询:仅查询deleted为0的
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(sort+" "+order);
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        if (!StringUtil.isEmpty(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if (!StringUtil.isEmpty(url)){
            criteria.andUrlLike("%"+url+"%");
        }
        criteria.andDeletedEqualTo(false);
        List<Keyword> list = keywordMapper.selectByExample(keywordExample);
        //传入查询结果list通过PageHelper获取total和pages
        PageInfo<Keyword> pageInfo = new PageInfo<>(list);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        return DataVO.data(total,pages,limit,page,list);
    }

    /**
     * @author: zhoukang
     * @description: TODO 添加关键字
     * @date: 2022/5/6 15:25
     */
    @Override
    public Keyword insert(Keyword keyword) {
        keyword.setAddTime(new Date());
        keyword.setUpdateTime(new Date());
        if (keyword.getUrl()==null){
            keyword.setUrl("");
        }
        if (keyword.getIsHot()==null){
            keyword.setIsHot(false);
        }
        if (keyword.getIsDefault()==null){
            keyword.setIsDefault(false);
        }
        keyword.setDeleted(false);
        keyword.setSortOrder(100);
        int id = keywordMapper.insertKeyword(keyword);
        return keyword;
    }

    /**
     * @author: zhoukang
     * @description: TODO 逻辑删除关键字
     * @date: 2022/5/6 17:32
     */
    @Override
    public void delete(Keyword keyword) {
        //逻辑删除
        keyword.setDeleted(true);
        //更新时间
        keyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKey(keyword);
    }

    /**
     * @author: zhoukang
     * @description: TODO 修改关键字信息
     * @date: 2022/5/6 17:47
     */
    @Override
    public Keyword update(Keyword keyword) {
        //更新时间
        keyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKey(keyword);
        return keyword;
    }


}
